var startTime
var endTime
var weightList = []
var bmiList = []
var bmrList = []
var skeletonList = []
var muscleMassList = []
var visceralList = []
var waterList = []
var fatRateList = []
var proteinList = []
var bodyAgeList = []
var timeList = []
var intervalList = []
var isData = false

$(document).ready(function () {
    GetRequestData(GetRequest())
    $("#chartBtnGroup button:first").addClass("active");
});

function GetRequestData(list){
    if(JSON.stringify(list) != "{}"){
        console.log(list['id'])
        $("#searchInput").val(list['id']);
        getData(list['id'])
    }
}

function getData(id){
    startTime = $('#start').val() != ''? $('#start').val():'2019-01-01';//开始时间
    endTime = $('#end').val() != ''? $('#end').val(): laydate.now(1, 'YYYY-MM-DD');//结束时间
    $.ajax({
        url: global.apiUrl + '/get_user_dataorder',
        type: 'GET',
        dataType: 'json',
        data: {
            start_time: startTime,
            end_time: endTime,
            userid: id,
        },
        header: {
            'content-type': 'application/json' //默认值
        },
        success: function(res){
            // console.log(res)
            $('.dataAre').css('display','none')
            getEchartsData(res.content)

        }
    })
}

function getEchartsData(list){
    if(list.length!=0){
        weightList = []
        bmiList = []
        bmrList = []
        skeletonList = []
        muscleMassList = []
        visceralList = []
        waterList = []
        fatRateList = []
        proteinList = []
        bodyAgeList = []
        timeList = []
        intervalList = []
        for(var i in list){
            var arr =  list[i].order_content.bz.concat(list[i].order_content.height).concat(list[i].order_content.low)
            intervalList.push(arr)
            weightList.push(list[i].weight?list[i].weight:'-')
            bmiList.push(list[i].bmi?list[i].bmi:'-')
            bmrList.push(list[i].bmr?list[i].bmr:'-')
            skeletonList.push(list[i].skeletoncontent?list[i].skeletoncontent:'-')
            muscleMassList.push(list[i].musclemass?list[i].musclemass:'-')
            visceralList.push(list[i].visceralfatrate?list[i].visceralfatrate:'-')
            waterList.push(list[i].watercontent?list[i].watercontent:'-')
            fatRateList.push(list[i].bodyfat?list[i].bodyfat:'-')
            proteinList.push(list[i].protein?list[i].protein:'-')
            bodyAgeList.push(list[i].bodyage?list[i].bodyage:'-')
            timeList.push(list[i].createtime)
        }
        // console.log(arr2)
        isData = true
        $("#chartBtnGroup button:first").click().addClass("active");
    }else{
        alert('无测量数据')
        // $("#chartBtnGroup button:first").addClass("active");
        isData = false
    }
}

function showChart(name){
    $("#chartBtnGroup").children().removeClass("active");
    $('.dataAre').css('display','none')
    // console.log(name)
    var title = ''
    var list = []
    switch (name) {
        case 'weight':
            title = '体重';
            list = weightList;
            $("#chartBtnGroup .chartBtn").eq(0).addClass("active");
            break;
        case 'bmi':
            title = 'BMI';
            list = bmiList;
            $("#chartBtnGroup .chartBtn").eq(1).addClass("active");
            break;
        case 'bmr':
            title = '基础代谢';
            list = bmrList;
            $("#chartBtnGroup .chartBtn").eq(2).addClass("active");
            break;
        case 'skeleton':
            title = '骨量';
            list = skeletonList;
            $("#chartBtnGroup .chartBtn").eq(3).addClass("active");
            break;
        case 'muscleMass':
            title = '肌肉量';
            list = muscleMassList;
            $("#chartBtnGroup .chartBtn").eq(4).addClass("active");
            break;
        case 'visceral':
            title = '内脏脂肪';
            list = visceralList;
            $("#chartBtnGroup .chartBtn").eq(5).addClass("active");
            break;
        case 'water':
            title = '水分率';
            list = waterList;
            $("#chartBtnGroup .chartBtn").eq(6).addClass("active");
            break;
        case 'fatRate':
            title = '体脂率';
            list = fatRateList;
            $("#chartBtnGroup .chartBtn").eq(7).addClass("active");
            break;
        case 'protein':
            title = '蛋白质率';
            list = proteinList;
            $("#chartBtnGroup .chartBtn").eq(8).addClass("active");
            break;
        case 'bodyAge':
            title = '体年龄';
            list = bodyAgeList;
            $("#chartBtnGroup .chartBtn").eq(9).addClass("active");
            break;
    }
    if(isData == true){
        setEcharts(title,list)
    }else{
        alert('暂无测量数据')
    }
}

function clickBtn(){
    if($("#searchInput").val()!= ''){
        getData($("#searchInput").val())
        $('.dataAre').css('display','none')
    }else{
        alert('请输入用户ID')
    }
}

function setEcharts(title,list) {
    var lineChart = echarts.init(document.getElementById("echarts-line-chart"));
    lineChart.on('click', echartsBtn);
    var lineoption = {
        title : {
            text: title + '变化趋势',
            subtext: startTime + '~' + endTime,
            textAlign: 'center',
            x: 'center'
        },
        tooltip : {
            trigger: 'axis',
            axisPointer : {
                type : 'line',
                lineStyle : {
                    color: '#bbb',
                    width: 1,
                    type: 'solid'
                },
            },
        },
        grid:{
            x:60,
            x2:60,
            y: 80,
            y2:24
        },
        // calculable : true,
        dataZoom:[
            {
              show: false,
              type:'slider',
              start: dataZoom(timeList),
              end: 100,
              left:'3%',
              right:'3%',
              maxValueSpan: 10,
            },
            {
              type: 'inside',
            },
        ],
        xAxis : [
            {
                show: true,
                type : 'category',
                boundaryGap : false,
                data : timeList,
                axisLine:{
                  show: true,
                  lineStyle:{
                    width:1,
                    color: 'black'
                  }
                },
                axisTick:{
                  show:true
                },
                splitLine:{
                  show: false,
                },
                axisLabel:{
                  show:true,
                  interval: 0,
                  formatter: '{value}',
                  formatter:function(value){
                    // return util.setTime13(value) + '日'
                    return value
                  }
                },
            }
        ],
        yAxis : [
            {
                type : 'value',
                show:true,
                axisLine:{
                  show: true,
                  lineStyle:{
                      width:1,
                      color: 'black'
                  }
                },
                axisTick: {
                  show: true,
                },
                splitLine:{
                  show: true,
                },
                axisLabel:{
                  show: true,
                  inside: false,
                  verticalAlign: 'middle',
                  color: 'black',
                  formatter:function(value,index){
                    return value + setUnit(title)
                  }
                },
            }
        ],

        series : [
            {
                name: title,
                type:'line',
                smooth: true,
                symbol: 'circle',
                symbolSize: 6,
                itemStyle : {
                  normal: {
                    color: 'rgb(26, 179, 148)',
                  },
                },
                emphasis:{
                    label:{
                      show: true,
                      position: 'top',
                      color: 'black',
                    },
                    itemStyle:{
                      borderWidth: 12,
                      borderType:'solid',
                      borderColor: 'rgba(26, 179, 148, 0.5)'
                    }
                },
                data: list
            },
        ]
    };
    lineChart.setOption(lineoption);
    $(window).resize(lineChart.resize);
}


function echartsBtn(param){
    // console.log(param)
    $('.dataAre').css('display','block')
    for(var i in intervalList[param.dataIndex]){
        if(param.seriesName === intervalList[param.dataIndex][i].title){
            var html = ''
            html += `<div class="dataAreMain">
                        <div class="dataItemAre">
                            <text>${param.seriesName}</text>
                            <text style="margin: 5px 0;">${param.value}${setUnit(param.seriesName)}</text>
                            <div class="tipAre" style="background:${getTipColor(intervalList[param.dataIndex][i].type)}">${intervalList[param.dataIndex][i].type}</div>
                        </div>
                        <div class="intervalAre">
                            <div class="MeasuringRangeAre">
                                ${setInterval(param.seriesName,intervalList[param.dataIndex][i].best)}
                            </div>
                            <div class="MeasuringRangeText">
                                <text >
                                    &emsp;&emsp;${setIntroduce(intervalList[param.dataIndex][i])}
                                </text>
                            </div>
                        </div>
                    </div>`
            $('.dataAre').html(html)
            break;
        }
    }
}
